<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\controller;

class Upload extends \app\BaseController
{
    public function upload(string $name = 'file')
    {
        $file = $this->request->file($name);

        /**
         * 文件大小检查
         * 文件类型是否允许上传
         * mime与ext是否对照
         * 检查是不是有木马
         */

        $savename = \think\facade\Filesystem::disk('public')->putFile($name, $file, function () use ($file) {
            return $file->getOriginalName() . '.' . $file->getOriginalExtension();
        });

        $icon = '/static/images/files/' . $file->getOriginalExtension() . '.png';
        if (is_file($icon)) {
            $icon = '/static/images/files/miss.png';
        }
        $fileUrl = [
            'url'      => $this->request->domain() . '/storage/' . $savename,
            'filename' => $file->getOriginalName(),
            'size'     => friendly_size($file->getSize()),
            'icon'     => $icon,
        ];
        return json(['data' => $fileUrl, 'code' => 200]);
    }
}
